package com.jy.controller;

import com.jy.common.SessionInfo;
import com.jy.model.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;

@Component("SpringMVCInterceptor")
public class AuthInterceptor extends HandlerInterceptorAdapter {
	 @Override    
	    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
	        request.setCharacterEncoding("UTF-8");    
 	        response.setCharacterEncoding("UTF-8");
	        response.setContentType("text/html;charset=UTF-8");    
	     
	        //过滤登录、退出访问    
	       // String[] noFilters = new String[] { "/finance/login.do", "/finance/member.do" };
		 String[] noFilters = new String[] { "/weixin" };

	        String uri = request.getRequestURI();   
	        StringBuffer url=request.getRequestURL();
	        boolean beFilter = true;    
	        for (String s : noFilters) {
	            if (uri.indexOf(s) != -1) {    
	                beFilter = false;    
	                break;    
	            }    
	        }
	        System.out.println((User) request.getSession(false).getAttribute(SessionInfo.SessionName));
	        User sessionContainer = (User) request.getSession(false).getAttribute(SessionInfo.SessionName);
	        if (beFilter) {    
	            if (null == sessionContainer) {    
	                //ajax方式交互    
	                if (request.getHeader("x-requested-with") != null    
	                        && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest"))// 如果是ajax请求响应头会有，x-requested-with；    
	                {                       
	                    response.setHeader("sessionstatus", "timeout");// 在响应头设置session状态    
	                    return false;    
	                }    
	                // 未登录    
	                PrintWriter out = response.getWriter();    
	                StringBuilder builder = new StringBuilder();    
	                builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");    
	                builder.append("alert(\"用户已于其他地方登录或登录失效，如果不是本人操作，请尽快修改密码！\");");    
	                builder.append("window.top.location.href='login.jsp';");
	                builder.append("</script>");    
	                out.print(builder.toString());    
	                out.close();    
	                return false;    
	            } else {                        
	                // 添加系统日志    
	                // -----------------------------------    
	                // -----------------------------------    
	            }    
	        }    
	        Map paramsMap = request.getParameterMap();    
	        return super.preHandle(request, response, handler);    
	    }    
}
